import React, { useState } from 'react'
// import useStore from '@/store'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { UserApi } from '@/api/user'
import { useUserStore } from '@/store/user'
import { parseUrlParams } from '@/utils/index'
import { useNavigate } from 'react-router-dom';

const Login: React.FC = () => {
  const [username, setUsername] = useState('stadmin');
  const [password, setPassword] = useState('112233');
  const navigate = useNavigate();
  const { setToken } = useUserStore();

  const handleLogin = async () => {
    const params: any = parseUrlParams(window.location.href)
    const data: any = await UserApi.login({
      username,
      password,
      tenantId: '489634',
      clientId: import.meta.env.VITE_APP_CLIENT_ID,
      grantType: 'password'
    });
    
    if (data.access_token) {
      setToken(data.access_token)
      navigate(params.redirectPath || '/dashboard');
    } else {
      // 处理登录失败
      console.error('Login failed:', data.msg); // 添加调试信息
    }
  };

  return (
    <div className='dark'>
      <div>登录</div>
      <Input
        type="text"
        value={username}
        onChange={(e) => setUsername(e.target.value)}
        placeholder="用户名"
      />
      <Input
        type="password"
        value={password}
        onChange={(e) => setPassword(e.target.value)}
        placeholder="密码"
      />
      <Button onClick={handleLogin}>登录</Button>
    </div>
  );
}

export default Login